/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package gui.objects3D;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Vector3f;

import utils.Position;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Primitive;

/**
 * The Class Buffer3D.
 * 
 */
public class Buffer3D extends Box {
	
	
	/** The constants for the dimensions of the loads. */
	public static final float LOADWIDTH = Load3D.LOADWIDTH;
	public static final float LOADLENGTH = Load3D.LOADLENGTH;
	public static final float LOADHEIGHT = Load3D.LOADHEIGHT;
	/** The constant for the the primflags. */
	private static int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
	/** The two different types of buffers (in, out). */
	public enum Type{IN, OUT;};
	
	
	/**
	 *  Instantiate a new Buffer3D object.
	 * 
	 * @param type the buffer type
	 */
	public Buffer3D(elements.Buffer.Type type){
		super(LOADLENGTH/2, 0, LOADWIDTH/2, primflags, null);
		this.setAppearance(createAppearance(type));
	}
	
	
	/**
	 * Create appearance.
	 * 
	 * @return the appearance
	 */
	private Appearance createAppearance(elements.Buffer.Type type) {
		Appearance app = new Appearance();
		ColoringAttributes colorAttr = new ColoringAttributes();
		// input buffer
		if (type == elements.Buffer.Type.IN){
			colorAttr.setColor(0f, 0f, 0f);
			app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.2f));
		} else { // output buffer
			colorAttr.setColor(1.0f, 1.0f, 1.0f);
			app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.2f));
		}
		app.setColoringAttributes(colorAttr);
		return app;
	}
	
	
	/**
	 * Set position of the buffer
	 * 
	 * @return the transform group
	 */
	public TransformGroup setPosition(Position bufferPosition){
		TransformGroup bufferObject = new TransformGroup();
		bufferObject.addChild(this);
		Transform3D bufferT3D = new Transform3D();
		bufferT3D.setTranslation(new Vector3f(bufferPosition.getX()+0.5f,0.0f,bufferPosition.getZ()+0.5f));
		bufferObject.setTransform(bufferT3D);
		return bufferObject;
	}
}
